home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: psycho@bridge.net (Gary Thompson)
- Newsgroups: comp.lang.c
- Subject: Re: Crash Proofing?
- Date: Fri, 15 Mar 1996 07:45:06 GMT
- Organization: A poorly-installed InterNetNews site
- Message-ID: <4ib79j$6gv@news.bridge.net>
- References: <4hsfje$rbr@uwm.edu>
- NNTP-Posting-Host: ppp-ftl1-28.bridge.net
- X-Newsreader: Forte Free Agent 1.0.81
-
- peterk@alpha2.csd.uwm.edu (Peter J Kleczka) wrote:
- >Hi all
- > I'm new to C programing and am trying to re-write a program
- >I did in pascal in C. When I wrote the pascal program I got
- >caught up in the details of getting the program to work and
- >consequentially readability of the code and user-friendlyness
- >suffered......
- > The program I'm working on (below) works fine as long
- >as the user enters an integer.....but it gets caught in an
- >endless loop between the functions: firstmenu() and firstchoice()
- >when I enter, say, 5.5 instead of an integer
- >it's unlikely that the user would enter anything but an integer
- >when presented with integer choices...but I want to make the
- >code uncrashable .....what can I do if anything to make it
- >so that the program doesnt do wierd things if the user enters
- >the wrong thing here?
-
- >/* prototype for the C version of block schedule program */
-
- > blah-blah-blah
-
- Call it a fault of scanf. I never use it. scanf REQUIRES the user to input
- ONLY the type requested.
-
- >firstchoice()
- >{
- > unsigned choice;
- > scanf ("%d", &choice);
- > while (choice !=1 && choice != 2 && choice != 3 && choice !=4)
- > {
- > printf("You entered %d ... please choose a number between 1-4\n\n", choice);
- > firstmenu(); /* remind user of appropos choices */
- > scanf("%d", &choice);
- > }
- >}
-
- Change it to this...
-
- firstchoice()
- {
- unsigned choice=0;
- char buff[10];
-
- do
- {
- firstmenu(); /* take firstmenu out of the MAIN function */
- gets(buff);
- choice=atoi(buff);
- }
- while(choice<1 || choice>4)
-
- }
- That's a little different. A bit shorter and will save on code space. strictly
- up to you tho... there are thousands of ways of doing it.
-
- Only problem with this is if the user enters a false-entry of more than 10 chars
- you will get a variable overload which may crash the program. You can fix that
- by replacing...
-
- gets(buff)
- with...
- fgets(stdin,buff,5);
-
- ( may have the order and name of the standard input constant wrong...) This is
- cheating but it works just fine. You are forcing the user to input only 5
- chars. I think it will allow you to input more but only 5 will be taken. This
- prevents an overload. The only way to only allow 5 chars to be input is to
- design your own input routine. That's not an easy task but the only way to make
- it 100% bullet-proof. (That's what I always do... it does make for extremely
- versatile programs...)
-
-
- Gary Thompson
- "The Psycho"
- psycho@bridge.net
- 72607.1365@compuserve.com
- http://www.bridge.net/~psycho
- HTTP://ourworld.compuserve.com/homepages/psychotps
-
-